package com.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.core.io.Resource;
/**
 * 系统参数工具类
 * @ClassName: SysParamUtil 
 */
public class SysParamUtil {
	
	public static Map<String,String> map = null;
	
	private static Properties p = null;
	
	private Resource[] locations;//源
	
	@SuppressWarnings("unused")
	private void init(){
		if(null == map || map.size() == 0){
	        p = new Properties();
	        map = new HashMap<String, String>();
	        InputStream is = null;
	        try {
	        	for (int i = 0; i < this.locations.length; ++i) {
		    		is = this.locations[i].getInputStream();
		        	//is = SysParamUtil.class.getResourceAsStream("/qp/config/sysParam.properties");
		            p.load(is);
		            for (Object key : p.keySet()){
		                String keyStr = key.toString();
		                String value = p.getProperty(keyStr);
		                map.put(keyStr, value);
		            }
	        	}
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
				if(null != is){
					try {
						is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
    }
	
	public static String getParam(String key){
//		if(null == map || map.size() == 0){
//			getInstance().init();
//		}
		if(null != map){
			if(null != map.get(key)){
				return map.get(key);
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	
	public static int getInt(String key){
		if(null != map){
			if(null != map.get(key)){
				return Integer.valueOf(map.get(key));
			}else{
				return 0;
			}
		}else{
			return 0;
		}
	}
	
	public void setLocations(Resource[] locations) {
		this.locations = locations;
	}
	
	
//    private SysParamUtil(){}
//    private static class LazyHolder {
//    	private static final SysParamUtil INSTANCE = new SysParamUtil();
//    }
//    public static final SysParamUtil getInstance(){
//       return LazyHolder.INSTANCE;
//    }

    
}
